﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Revis.VClinicNet.App.Internal;
using Revis.VClinicNet.App.Internal.Dto;
using Revis.VClinicNet.UI.Internal.Mvc.Models.Forms;
using Revis.VClinicNet.App.Internal.Dto.Validation;
using Revis.VClinicNet.UI.Internal.Mvc.Extensions;
using Revis.VClinicNet.UI.Internal.Mvc.Infrastructure;
using Revis.VClinicNet.Core.Domain.Credentials;

namespace Revis.VClinicNet.UI.Internal.Mvc.Controllers
{
    public class EntryController : Infrastructure.ControllerBase
    {
        private IEntryService _entryService = null;
        private IVisitService _visitService = null;

        public EntryController(IEntryService ies, IVisitService ivs)
        {
            _entryService = ies;
            _visitService = ivs;
        }

        [HttpGet]
        [HistoryBackLinked]
        [AuthorizeFor(Role= WorkerUserRole.Administrator | WorkerUserRole.Doctor | WorkerUserRole.Recepcionist)]
        public ActionResult Index()
        {
            return View();
        }


        [HttpGet]
        [HistoryBackLinked]
        [AuthorizeFor(Role = WorkerUserRole.Administrator | WorkerUserRole.Doctor)]
        public ActionResult Create(long id)
        {
            VisitInfoDto visit = null;
            EntryCreateDto entry = null;
            try {
                visit = _visitService.Find<VisitInfoDto>(id);
                entry = new EntryCreateDto() {
                    DoctorId = UserId,
                    VisitId = visit.Id
                };
            } catch (ClinicException ce) {
                return View("Error", new HandleErrorInfo(ce, "Entry", "Create"));
            }
            return View(new EntryCreateViewModel(visit, entry));
        }

        [HttpPost]
        [HistoryBackLinked(Type=BackLinkType.ModifyForm)]
        [AuthorizeFor(Role = WorkerUserRole.Administrator | WorkerUserRole.Doctor)]
        public ActionResult Create(EntryCreateDto entry)
        {
            long id = -1;
            try {
                id = _entryService.Create(entry);
            } catch (ValidationException ve) {
                ModelState.AddModelErrors(ve);
            } catch (ClinicException ce) {
                ModelState.AddErrorMessage(ce.Message);
            }
            if (ModelState.IsValid == false) {
                VisitInfoDto visit = null;
                try {
                    visit = _visitService.Find<VisitInfoDto>(entry.VisitId);
                } catch (ClinicException ce) {
                    return View("Error", new HandleErrorInfo(ce, "Entry", "Create"));
                }
                return View(new EntryCreateViewModel(visit, entry));
            }
            return RedirectToAction("Detail", new { id = id });
        }

        [HttpGet]
        [HistoryBackLinked]
        [AuthorizeFor(Role = WorkerUserRole.Administrator | WorkerUserRole.Doctor)]
        public ActionResult Detail(long id)
        {
            EntryDto entry = null;
            try {
                entry = _entryService.Find<EntryDto>(id);
            } catch (ClinicException ce) {
                return View("Error", new HandleErrorInfo(ce, "Entry", "Detail"));
            }
            return View(entry);
        }

        [HttpGet]
        [HistoryBackLinked]
        [AuthorizeFor(Role = WorkerUserRole.Administrator | WorkerUserRole.Doctor)]
        public ActionResult PatientCard(long id)
        {
            IList<EntryInfoDto> entries = null;            
            entries = _entryService.PatientCard(id);
            return View(entries);
        }

    }
}
